package com.work.game.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.work.game.R;
import com.work.game.home.ui.activity.GoodDetailActivity;
import com.work.game.home.ui.model.Good;

import java.util.ArrayList;
import java.util.List;

public class ItemGoodsStyle1Adapter extends RecyclerView.Adapter<GoodsItemViewHolder> {
    List<Good> goodsList=new ArrayList<>();
    Context context;

    public ItemGoodsStyle1Adapter( Context context,List<Good> goodsList) {
        this.goodsList = goodsList;
        this.context = context;
    }

    @NonNull
    @Override
    public GoodsItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_goods_style1, parent, false);
        return new GoodsItemViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsItemViewHolder holder, int position) {
        Good good = goodsList.get(position);
        Glide.with(context)
                .load(good.getPgUrl())
                .fitCenter()
                .apply(new RequestOptions().transform(new RoundedCorners(32)))
                .error(R.drawable.image_error)
                .into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GoodDetailActivity.class);
                intent.putExtra("id", good.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }


}

class GoodsItemViewHolder extends  RecyclerView.ViewHolder{
    ImageView imageView;
    public GoodsItemViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView=itemView.findViewById(R.id.pic);
    }
}